package com.hongkou.tvstation.support.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hongkou.tvstation.common.CodeCst;
import com.hongkou.tvstation.support.dao.UserDao;
import com.hongkou.tvstation.support.data.TUser;

public class UserManagementAction extends Action {
	private static final String ACTION_METHOD = "action_method";

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if ("login".equals(request.getParameter(ACTION_METHOD))) {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			TUser tUser = userDao.findUser(username, password);
			if (tUser != null) {
				request.getSession().setAttribute(CodeCst.ATTRIBUTE__USER,
						tUser);
			} else {
				return mapping.findForward(CodeCst.FORWARD__USER_INDEX_PAGE);
			}
		} else if ("logout".equals(request.getParameter(ACTION_METHOD))) {
			return mapping.findForward(CodeCst.FORWARD__USER_INDEX_PAGE);
		}
		return mapping.findForward(CodeCst.FORWARD__IMAGEMANAGEMENT);
	}

	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

}
